package com.oa.controller;


import com.oa.oaspccommon.dto.ActionResult;
import com.oa.oaspccommon.dto.UserInfo;
import com.oa.oaspccommon.po.User;
import com.oa.oaspcpersonalcenterapi.api.UserServiceApi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author AkazaAkari
 * @version 1.0
 * @className MailApiController
 * @description TODO
 * @date 2021/4/13 16:32
 */
@RestController
public class UserApiController {
    @Autowired(required = false)
    private UserServiceApi userServiceApi;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ActionResult login(@RequestBody UserInfo userInfo){
       User user=userServiceApi.login(userInfo);
       return new ActionResult(200,"登陆成功",user);
//        ActionResult result = null;
////       String checkPass=new Md5Hash(userInfo.getPassword(),"oasystem",1024).toString();
//        String checkPass = userInfo.getPassword();//未加密
//        User user = userServiceApi.login(userInfo);
//        if (user == null || !checkPass.equals(user.getPassword())) {
//            return result = new ActionResult(400, "login false", null);
//        }
//        //登录成功 0为用户 1为管理
//        if (user.getPassword().equals(checkPass) && user.getJurisdictiongroup().equals("0")) {
//            String token = JwtUtil.createToken(user.getUsername());
//            result = new ActionResult(200, "user login success", token);
//
//        } else if (user.getPassword().equals(checkPass) && user.getJurisdictiongroup().equals("1")) {
//            String s = JwtUtil.createToken(user.getUsername());
//            result = new ActionResult(300, "admin login success", s);
//        }
//        return result;
    }
    //修改密码
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public ActionResult changePassword(@RequestBody UserInfo userInfo) {
        ActionResult actionResult = userServiceApi.changePassword(userInfo);
        return actionResult;
    }

    @GetMapping("/users")
    public ActionResult findUsers(){
        List<User> all=userServiceApi.findUsers();
        return new ActionResult(200,"",all);

    }
    @GetMapping("/user/{uid}")
    public User findUserByUid(@PathVariable("uid") int uid) {
        User user = userServiceApi.findUserByUid(uid);
        return user;
    }

    @PostMapping("/user")
    public boolean addUser(@RequestBody User user) {
        boolean b = userServiceApi.addUser(user);
        return b;
    }

    @DeleteMapping("/user")
    public boolean deleteUserByUid(@RequestParam(value = "uid") int uid) {
        boolean b = userServiceApi.deleteUserByUid(uid);
        return b;
    }

    @PutMapping("/user")
    public boolean updateUser(@RequestBody User user) {
        boolean b = userServiceApi.updateUser(user);
        return b;
    }

}
